////////////////////////////////////////////////////////////////////////////////
/// @file    wav_operate.c
/// @author  AE TEAM
/// @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////
// Define to prevent recursive inclusion
#define _WAV_OPERATE_C_

// Files includes
// Define to prevent recursive inclusion
#define _WAV_OPERATE_C_

// Files includes

#include "HAL_conf.h"
#include "HAL_device.h"
#include <stdio.h>



#include "wav_operate.h"


#include "sdio.h"
#include "ff.h"
#include "fatfs.h"
#include "sdio_sdcard.h"
#include "uart.h"
#include "delay.h"
#include "wav_operate.h"
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Example_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MAIN
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MAIN_Exported_Constants
/// @{
REC_T g_tRec;



#define  SIZE  (8*1024)


uint16_t usData[SIZE];
//uint32_t usData1[SIZE];
#define DataFormat_16   1
#define DataFormat_24   0
#define DataFormat_32   0

#define Standard_Phillips   1
#define Standard_MSB   0
#define Standard_LSB   0
#define Standard_PCM   0

u8 buffer1[512] = {0x00};

u32 AudioTotalSize = 0xFFFF;
u32 AudioRemSize  = 0;
u16* CurrentPos;


__IO u32 WaveLen = 0;
//__IO u32 XferCplt = 0;
__IO u32 DataOffset = 0;
__IO u32 WaveCounter;
u8 buffer_switch = 1;
WAVE_TypeDef WAVE_Format;

typedef __packed struct {
    u32 ChunkID;
    u32 ChunkSize ;
    u32 Format;
} ChunkRIFF ;

u8 WaveParsing( u8* pbuffer)
{
    u32 temp = 0x00;
    u32 extraformatbytes = 0;

    temp = ReadUnit((u8*)pbuffer, 0, 4, BigEndian);
    if(temp != CHUNK_ID)
        return 1;
    WAVE_Format.RIFFchunksize = ReadUnit((u8*)pbuffer, 4, 4, LittleEndian);
    temp = ReadUnit((u8*)pbuffer, 8, 4, BigEndian);
    if(temp != FILE_FORMAT)
        return 2;
    temp = ReadUnit((u8*)pbuffer, 12, 4, BigEndian);
    if(temp != FORMAT_ID)
        return 3;
    temp = ReadUnit((u8*)pbuffer, 16, 4, LittleEndian);
    if(temp != 0x10)
        extraformatbytes = 1;
    WAVE_Format.FormatTag = ReadUnit((u8*)pbuffer, 20, 2, LittleEndian);
    if(WAVE_Format.FormatTag != WAVE_FORMAT_PCM)
        return 4;
    WAVE_Format.NumChannels = ReadUnit((u8*)pbuffer, 22, 2, LittleEndian);
    WAVE_Format.SampleRate = ReadUnit((u8*)pbuffer, 24, 4, LittleEndian);
    WAVE_Format.ByteRate = ReadUnit((u8*)pbuffer, 28, 4, LittleEndian);
    WAVE_Format.BlockAlign = ReadUnit((u8*)pbuffer, 32, 2, LittleEndian);
    WAVE_Format.BitsPerSample = ReadUnit((u8*)pbuffer, 34, 2, LittleEndian);
    if(WAVE_Format.BitsPerSample != BITS_PER_SAMPLE_16)
        return 5;
    DataOffset = 36;
    if(extraformatbytes == 1) {
        temp = ReadUnit((u8*)pbuffer, 36, 2, LittleEndian);
        if(temp != 0x00)
            return 6;
        temp = ReadUnit((u8*)pbuffer, 38, 4, BigEndian);
        if(temp != FACT_ID)
            return 7;
        temp = ReadUnit((u8*)pbuffer, 42, 4, LittleEndian);
        DataOffset += 10 + temp;
    }
    DataOffset = 36;
    temp = ReadUnit((u8*)pbuffer, DataOffset, 4, BigEndian);
    DataOffset += 4;
    if(temp != DATA_ID)
        return 8;
    WAVE_Format.DataSize = ReadUnit((u8*)pbuffer, DataOffset, 4, LittleEndian);
    DataOffset += 4;
    WaveCounter = DataOffset;
    return 0;
}

u32 ReadUnit(u8* buffer, u16 idx, u8 NbrOfBytes, Endianness BytesFormat)
{
    u32 index = 0;
    u32 temp = 0;

    for(index = 0; index < NbrOfBytes; index++)temp |= buffer[idx + index] << (index * 8);
    if (BytesFormat == BigEndian)temp = __REV(temp);
    return temp;
}
FIL fileR;
UINT BytesRead;
#include "string.h"

void readwavfile(void)
{
    FRESULT res_sd;

    DELAY_Init();
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOE, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_SDIO, ENABLE);
    CONSOLE_Init(115200);

    SDIO_ConfigInit();
    printf("SDCARD TEST\r\n");
    while(SD_Init()) {
        printf("SD Card Error!\r\n");
        DELAY_Ms(500);
    }
    MX_FATFS_Init();
    f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);

    do {
        res_sd = f_open(&fileR, "0:/song.wav", FA_READ);
    } while(FR_OK != res_sd);
    WaveLen = 0;
    f_read(&fileR, buffer1, 512, &BytesRead);
    while(1) {
        if(WaveParsing(buffer1) == 0) {
            break;
        }
    }
    WaveLen = WAVE_Format.DataSize;
}

/// @}

/// @}

/// @}




